<%@ page import="com.example.util.DBUtil" %>
<%@ page import="com.example.dao.UserDao" %>
<%@ page import="com.example.domain.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page pageEncoding="UTF-8"%>
<%
    // 设置请求编码
    request.setCharacterEncoding("UTF-8");
    
    // 获取用户输入
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    
    // 检查输入是否为空
    if (username == null || username.trim().isEmpty() || 
        password == null || password.trim().isEmpty()) {
        response.sendRedirect("login.jsp?error=用户名和密码不能为空");
        return;
    }
    
    try {
        UserDao userDao = new UserDao();
        User user = userDao.authenticate(username, password);
        
        if (user != null) {
            // 登录成功，将用户信息存储到session中
            session.setAttribute("user", user);
            response.sendRedirect("../index.jsp");
        } else {
            // 登录失败，返回登录页面并显示错误信息
            response.sendRedirect("login.jsp?error=invalid");
        }
    } catch (Exception e) {
        response.sendRedirect("login.jsp?error=系统错误: " + e.getMessage());
    }
%>